﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    public class Subdivision : ISerializable
    {
        private int idField;

        private string nameField;

        private static readonly Subdivision theOneObject = new Subdivision();

        public static Subdivision GetSubdivision()
        {
            return theOneObject;
        }
        public int ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("ID", idField);
            info.AddValue("Name",nameField);
        }
        /*
        public Subdivision (SerializationInfo info, StreamingContext context)
        {
            idField = (int) info.GetValue("ID", typeof (int));
            nameField = (string)info.GetValue("Name", typeof(string));
        }
         
        public  Subdivision (int ID, string Name)
        {
            this.idField = ID;
            this.nameField = Name;
        }
         */
        public  Subdivision ()
        {
        }

        public List<Classes.Subdivision> getSubdivisionList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {
            var sSub = new List<Classes.Subdivision>(from sb in staffEntryCollection select sb.Branch);
            var ssSub = sSub
                .GroupBy(s => new { s.ID })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }
        public List<Classes.Subdivision> getSubdivisionList(List<Classes.CardRecord> sCardRecord)
        {

            var sSubdivision = new List<Classes.Subdivision>(from lv in sCardRecord select lv.RecordSubdivision);

            var ssSub = sSubdivision
                .Where(s => s != null)
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }
    }
    
}
